import categoryApi from "@/api/category"
const state = {
    category1List: [],// 一级分类列表
    category2List: [],// 二级分类列表
    category3List: [],// 三级分类列表
    category1Id: '',// 一级分类id
    category2Id: '',// 二级分类id
    category3Id: '',// 三级分类id
}
const mutations = {
    SET_CATEGORY1_DATA(state, category1List) {
        state.category1List = category1List
    },
    // 改变1id的值
    SET_1ID(state, category1Id) {
        state.category1Id = category1Id
    },
    // 设置我们的二级分类列表
    SET_CATEGORY2_DATA(state, category2List) {
        state.category2List = category2List
    },
    // 改变2id的值
    SET_2ID(state, category2Id) {
        state.category2Id = category2Id
    },
    // 三级分类列表
    SET_CATEGORY3_DATA(state, category3List) {
        state.category3List = category3List
    },
    // 改变3id的值
    SET_3ID(state, category3Id) {
        state.category3Id = category3Id
    },
}
const actions = {
    // 获取我们的一级分类id
    async getCategory1(context) {
        // context.state.category2List = []
        // this.category3List = []
        // console.log(context);
        if (!context.state.category2Id) {
            context.commit('SET_2ID', '')
        }
        const res = await categoryApi.getCategory1()
        // console.log(res.data);
        context.commit('SET_CATEGORY1_DATA', res.data)
    },
    // 获取二级分类数据
    async getCategory2(context, category1Id) {
        // this.category3List = []
        context.state.category3Id = ''
        // console.log(category1Id);
        try {
            const res = await categoryApi.getCategory2(category1Id)
            // console.log(res);
            context.commit('SET_CATEGORY2_DATA', res.data)
        } catch (error) {
            // console.log(error);
        }
    },
    // 获取三级分类数据
    async getCategory3(context, category2Id) {
        context.state.category3Id = ''
        // console.log(category1Id);
        try {
            const res = await categoryApi.getCategory3(category2Id)
            // console.log(res);
            context.commit('SET_CATEGORY3_DATA', res.data)
        } catch (error) {
            // console.log(error);
        }
    }
}
const getters = {

}

export default {
    state,
    mutations,
    actions,
    getters,
}